// pages/mine/myform/confirmOrder/confirmOrder.js
const app = getApp();
const util = require("../../../../utils/util.js");
Page({
  data: {
    addressList: [],
    productMsg: [],
    yunfei: "",
    discount: "",
    shouldPay: "",
    productId: "",
    sizeId: "",
    addressId: "",
    productName: "",
    orderId: "",
    money: "",
    windowWidth: 0
  },

  // 点击提交订单
  submitOrder: function(e) {
    let that = this
    wx.showLoading()
    // 发送提交订单请求
    let submitUrl = app.globalData.baseUrl + "weixin/submitOrder"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid,
      addressId: that.data.addressId,
      productId: that.data.productId,
      yunfei: that.data.yunfei,
      discount: that.data.discount,
      sizeId: that.data.sizeId,
      
    }
    util.requestLoading(submitUrl, data, "GET").then(res => {
      // console.log(res)
      if(res.data.status == 0) {
        that.setData({
          productName: res.data.productName,
          orderId: res.data.orderId,
          money: res.data.money
        })
        // 发送支付请求
        let payUrl = app.globalData.baseUrl + "weixin/pay"
        let openid = wx.getStorageSync('openId')
        let data = {
          openid: openid,
          // productName: that.data.productName,
          orderId: that.data.orderId,
          money: that.data.money
        }
        util.requestLoading(payUrl, data, "GET").then(res => {
          // console.log(res)
          if (res.statusCode == 200) {
            
            let openId = wx.getStorageSync("openId")
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: 'MD5',
              paySign: res.data.paySign,
              success(res) {
                wx.hideLoading()
                wx.showModal({
                  content: '支付成功',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      wx.reLaunch({
                        url: '../../../index/index'
                      })
                    }
                  }
                })
              },
              fail(res) {
                wx.hideLoading()
                wx.showModal({
                  content: '未支付，已将订单加入待支付列表',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      wx.reLaunch({
                        url: '../../../index/index'
                        // url: '../../../index/index'
                      })
                    }
                  }
                })
              }
            })
          } 
        })
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '提交失败',
          content: '订单异常，请在订单列表中查看',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                // url: '../myform',
                url: '../myform?activeIndex=1&sliderOffset=69'
                // url: '../myform?activeIndex=1&sliderOffset=' + (that.data.windowWidth * 0.1875)
              })
            }
          }
        })
      }
    })
  },

  // 点击加入购物车
  toShopCar: function(e) {
    let that = this
    // 发送添加到购物车请求
    wx.showLoading()
    let addToUrl = app.globalData.baseUrl + "weixin/addToCart"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid,
      productId: that.data.productId,
      sizeId: that.data.sizeId,
      addressId: that.data.addressId
    }
    util.requestLoading(addToUrl, data, "GET").then(res => {
      // console.log(res)
      if (res.data.status == 0){ 
        wx.hideLoading()
        wx.reLaunch({
          url: '../../../shopCar/shopCar',
        })
      } else {
        wx.hideLoading()
        wx.showModal({
          title: '加入购物车异常',
          content: '添加失败',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '../../../index/index',
              })
            }
          }
        })
      }
    })
  },

  onLoad: function (options) {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        that.setData({
          windowWidth: res.windowWidth
        })
      },
    })
    that.setData({
      productId: options.productId,
      sizeId: options.sizeId,
      addressId: options.addressId
    })
    console.log(that.data.productId)
    // 请求订单信息
    let orderUrl = app.globalData.baseUrl + "weixin/sureOrder"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid,
      productId: options.productId,
      sizeId: options.sizeId,
      addressId: options.addressId
    }
    util.requestLoading(orderUrl, data, "GET").then(res => {
      // console.log(res)
      that.setData({
        addressList: res.data.addressList,
        productMsg: res.data.productMsg,
        yunfei: res.data.yunfei,
        discount: res.data.discount,
        shouldPay: res.data.shouldPay
      })
    })
  },
})